package thread;

public class Test8 {
    //串行执行
    private static void serial() {
        long beg = System.currentTimeMillis();
        long a = 0;
        for (long i = 0; i < 10_0000_0000; i++) {
            a++;
        }
        long b = 0;
        for (long i = 0; i < 10_0000_0000; i++) {
            b++;
        }
        long end = System.currentTimeMillis();
        System.out.println("消耗时间：" + (end - beg) + " ms");
    }

    public static void concurrency() throws InterruptedException {
        long beg = System.currentTimeMillis();
        Thread thread = new Thread(() -> {
            long a = 0;
            for (long i = 0; i < 10_0000_0000; i++) {
                a++;
            }
        });
        thread.start();
        Thread thread2 = new Thread(() -> {
            long b = 0;
            for (long i = 0; i < 10_0000_0000; i++) {
                b++;
            }
        });
        thread2.start();

        //join的效果就是等待线程结束. thread.join意思就是等到thread执行完了，才会返回，继续往下走.
        //等上面的两个线程执行完,再进行计时
        thread.join();
        thread2.join();

        long end = System.currentTimeMillis();
        System.out.println("消耗时间：" + (end - beg) + " ms");
    }

    //并发执行
    public static void main(String[] args) throws InterruptedException {
        //  serial();
        concurrency();
    }
}
